package com.chahu.filter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.chahu.entity.User;

import java.io.IOException;

@WebFilter("/static/backend/*")
public class BackendFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        String uri = req.getRequestURI();
        System.out.println("uri: " + uri);
        if(!uri.endsWith(".js") && !uri.endsWith(".css") && !uri.contains("img")
        	&& !uri.contains("login") && !uri.contains("logout")){
        	User admin = (User)req.getSession().getAttribute("admin");
        	System.out.println(admin);
        	if(admin == null)
        	{
        		res.sendRedirect("login.html");
        	}
        }
        chain.doFilter(req, res);
    }
}
